﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Model.ZXBuiness;
using Xproject.Framework.Core.Repositories;
using Xproject.Framework.Utility.Extensions;
namespace Repository.ZXBuiness
{
    public static class ZXTiXianRep
    {
        public static ZXTiXianApply GetById(this IRepositoryAsync<ZXTiXianApply> repository, string TiXianId)
        {
            Expression<Func<ZXTiXianApply, bool>> express = i => 1 == 1;

            express = express.AndAlso(i => i.TiXianId == TiXianId);

            return repository.Query().Where(express).Select().FirstOrDefault();
        }


        public static List<ZXTiXianApplyModel> GetTiXianApplyList(this IRepositoryAsync<ZXTiXianApply> repository, int offset, int limit, string ApplyUserCode)
        {
            string whereCondit = string.Format(@" where  1=1 ");

            if (!string.IsNullOrEmpty(ApplyUserCode))
            {
                whereCondit += string.Format(@" and zuf.ZX_UserId ={0} ", ApplyUserCode);
            }

            string exeTotalSql = string.Format(@"
             declare   @TOTAL INT
              SELECT  @TOTAL =count(zta.TiXianId )  
              FROM ZX_TiXianApply  zta  
                {0}
            ", whereCondit);

            string exeSql = string.Format(@"
        select * from ( 
             SELECT ROW_NUMBER() OVER(order by ApplyTime desc,zta.[State] desc) AS RowId,wu.NickName,zufr.FenRunMoney,
wo.OrderCode, zta.[TiXianId]
                      ,zta.[UserFenRunId]
                      ,zta.[ZX_UserId]
                      ,zta.[OrderFengyunId]
                      ,zta.[ApplyTime]
                      ,zta.[State]
                      ,zta.[ApplyUserCode]
                      ,zta.[ApproveOper]
                      ,zta.[ApproveTime]
                      ,zta.[ApproveMoney]
                      ,zta.[BalanceCode]
                      ,zta.[Remarks]
                       ,zufr.FRcode
                      ,@TOTAL as total
					    FROM ZX_TiXianApply  zta  
						inner join ZX_UserFenRun zufr
						on zufr.UserFenRunId = zta.UserFenRunId
						inner join ZX_Order_FenRunInfo zofr
						on zofr.OrderFengyunId =  zta.[OrderFengyunId]
						inner join WeiXin_Order wo
						on wo.orderId = zofr.orderId
						inner join ZX_UserInfo ZU
						ON ZU.ZX_UserId = zta.ZX_UserId
						INNER JOIN WeiXin_User wu
						on wu.UserID = zu.WX_UserId
                {0}
            )  as b
         where RowId between {1} and {2} 
            ", whereCondit, offset, offset + limit);
            return repository.SqlQueryT<ZXTiXianApplyModel>(exeTotalSql + exeSql).ToList();
        }

        public static ZXTiXianApply GetByUFId(this IRepositoryAsync<ZXTiXianApply> repository, long UserFenRunId)
        {
            Expression<Func<ZXTiXianApply, bool>> express = i => 1 == 1;

            express = express.AndAlso(i => i.UserFenRunId == UserFenRunId);

            return repository.Query().Where(express).Select().FirstOrDefault();
        }

    }
}
